package com.hellojavaweb.servlet;
import com.hellojavaweb.bean.Money;
import com.hellojavaweb.bean.User;
import com.hellojavaweb.dao.UserDao;

import javax.servlet.ServletContext;
import javax.servlet.http.HttpSession;
import java.io.IOException;
import java.sql.*;
import java.util.ArrayList;
import java.util.List;

/**
 * Created by daitiantian on 2017/1/12.
 */
public class LoginServlet_dtt extends javax.servlet.http.HttpServlet {

    protected void doPost(javax.servlet.http.HttpServletRequest request, javax.servlet.http.HttpServletResponse response) throws javax.servlet.ServletException, IOException {

//        String username = request.getParameter("username");
//        String pass = request.getParameter("pass");
//        System.out.println(username);
//        System.out.println(pass);
        ///////////////////////////////////////////////////////////////////////////////////////////////////////////
        //接收用户名和密码
        UserDao userDao = new UserDao();
        //连接数据库

        String username = null;
        String pass = null;


        username = request.getParameter("username");
        pass = request.getParameter("pass");

        if (username == null || username.equals("") || pass == null || pass.equals("")) {
            ServletContext application = request.getServletContext();
            application.setAttribute("message", "用户名或密码为空");


            response.sendRedirect("err_dtt.jsp");
            return;
        }

        String re = null;
        try {
            //操作数据库
            re = userDao.selDataBase(username);
        } catch (SQLException e) {
            e.printStackTrace();
        }

        if (re == null) {
            ServletContext application = request.getServletContext();

            application.setAttribute("message", "用户名不存在");

            response.sendRedirect("err_dtt.jsp");

        } else
        {
            if (re.equals(pass)) {
                //合法
                //将用户信息存入session
                HttpSession session = request.getSession();
                session.setAttribute("username", username);
                Money userMoney = new Money();
                try {
                    userMoney = userDao.slmDataBase(username);
                } catch (SQLException e) {
                    e.printStackTrace();
                }
                session.setAttribute("userMoney", userMoney);
                List<User> userList = new ArrayList<User>();


                try {
                    userList = userDao.slaDataBase();
                } catch (SQLException e) {
                    e.printStackTrace();
                }
//                System.out.println(userList.size());

                session.setAttribute("list", userList);

                List<Money> moneyList = new ArrayList<Money>();


                try {
                    moneyList = userDao.sla2DataBase();
                } catch (SQLException e) {
                    e.printStackTrace();
                }

                session.setAttribute("money", moneyList);

                //跳到下一个页面【servlet提供了两种：sendredirect转向，forward转发】
                //sendredirect 的 url 应该这样：/web应用名/servlet url
                response.sendRedirect("main_dtt.jsp");
            } else {
                //跳回登录页面

                ServletContext application = request.getServletContext();
                application.setAttribute("message", "密码错误");

                response.sendRedirect("err_dtt.jsp");
            }


        }

    }

        ///////////////////////////////////////////////////////////////////////////////////////////////////////////

//          else {
//            if (username.equals("admin") && pass.equals("admin")) {
//                //            response.getWriter().append("success");
//                //添加参数
////                ServletContext application = request.getServletContext();
////                application.setAttribute("username", username);
//                HttpSession session = request.getSession();
//                session.setAttribute("username", username);
//                response.sendRedirect("main_dtt.jsp");
//            } else {
//                response.getWriter().append("failed");
//            }
//        }

    protected void doGet(javax.servlet.http.HttpServletRequest request, javax.servlet.http.HttpServletResponse response) throws javax.servlet.ServletException, IOException {

//        response.getWriter().append("This is from here");

        //添加参数
//        ServletContext application = request.getServletContext();
//        application.setAttribute("username",);


        //直接跳转到index.jsp页面
//        response.sendRedirect("main.jsp");

        //转发请求到别的页面
//        RequestDispatcher errorPage = request.getRequestDispatcher("indexServlet");
//        errorPage.forward(request, response);
        doPost(request ,response);
    }
}
